const utils = require("../utils/utils");
const BuildMoudle = require("../module/BuildModule");
const path = require("path");
const walk = require("../utils/walk");
const Define = require("../define");

class SchemeBuilder {
    /**
     * @type {BuildMoudle}
     */
    module;

    /**
     * 初始化信息
     * AreaConfig.json
     */
    info;

    /**
     * info文件当前路径
     */
    dir;

    /**
     * 模版
     * @type {string}
     */
    temp;

    old;

    /**
     *
     * @param {string} dir
     * @param {*} info
     * @param {BuildMoudle} mod
     * @returns
     */
    build(dir, info, mod) {
        this.dir = dir;
        this.info = info;
        this.module = mod;

        const oldid = this.info.OldGameName ?? this.info.OldGameID;
        this.temp = utils.readObj(mod.tempPath);
        this.old = walk.dirWalkFiles(mod.inputPath, this.getOldFileName(oldid));
        const isMah = this.info.GameType === "MaJiang";
        const isCard = !isMah;

        // let result = walk.dirWalk(dir, Define.FILE_OPTION) ?? {};
        let result = {};
        for (let key in this.temp) {
            let oldItem = this.old[key];
            if (key == "statistics") {
                result[key] = {};
                if (oldItem instanceof Array) {
                    for (let sKey of oldItem) {
                        result[key][sKey] = this.temp[key][sKey];
                    }
                } else {
                    for (let sKey in oldItem) {
                        result[key][sKey] = oldItem[sKey];
                    }
                }
            } else {
                result[key] = oldItem;
            }
        }

        this.removeKey(result);

        this.write(result);

        //只有麻将处理音频资源包配置
        if (isMah) {
            const soundId = result.dialect;
            const soundBundleName = `mengqi_mah_sound_${soundId}`;
            const bundles = this.info.Loader.Depend.Load;

            //清理错误bundle
            for (let i = bundles.length - 1; i >= 0; i--) {
                let item = bundles[i];
                if (item.includes("mengqi_mah_sound_")) {
                    bundles.splice(i, 1);
                }
            }

            //写入bundle名称
            this.info.GameSoundBundle = soundBundleName;
            if (bundles && !bundles.includes(soundBundleName)) {
                bundles.push(soundBundleName);
            }

            const name = path.basename(this.dir);
            let file = path.join(this.module.outputPath, name, Define.DIR_CONFIG, Define.FILE_GAME_INFO);
            utils.writeFile(file, this.info, false, true);
        }
    }

    getOldFileName(id) {
        return [id + "_room.cfg", id + "_room.json"];
    }

    /**
     * 删除字段
     * @param {*} result
     */
    removeKey(result) {
        delete result.chat;
    }

    write(result) {
        const name = path.basename(this.dir);
        let file = path.join(this.module.outputPath, name, Define.DIR_CONFIG, Define.FILE_OPTION);
        utils.writeFile(file, result, false, true);
    }
}

module.exports = SchemeBuilder;
